home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 895 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.8 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: David Byrden <Goyra@iol.ie>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Function object operator() is too anonymous.
  5. Date: 28 Mar 1996 17:35:41 PST
  6. Organization: Ireland On-Line
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4jeupm$v60@nuacht.iol.ie>
  9. References: <4jerpo$bvb@ugress.uib.no>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 28 Mar 1996 21:02:46 GMT
  12. X-Mailer: Mozilla 1.22KIT (Windows; I; 16bit)
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMVs+bky4NqrwXLNJAQFMxgH+MIVbVqqUCW+R5AhZ/MKFQ5r4CPyXS51d
  15.     b4cy3e/3wPXPgBXBff9DNV9tqhb4zzSKEnYY3hHl7D1V0ECY5hnMEQ==
  16.     =dEkm
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. boukanov@sentef1.fi.uib.no (Igor Boukanov) wrote:
  20.  
  21. >   I noticed that that it impossible to write Function object class 
  22. >(DWP 20.3) that can be used for example as Arithmetic operation object 
  23. >and Predicate object in the same time, because both require operator() 
  24. >that can not be overloaded! 
  25.  
  26. >So why do not have instead of one anonymous 
  27. >operator() a set of function obect methods like 
  28. >unary_operation, binary_operation, predicate, comparison 
  29. >and each function from algorithm.h will call correspondent method. 
  30. >For example, find_if will call predicate method of function object and 
  31. >transform will call unary_operation or binary_operation 
  32. >instead of operator().
  33. >
  34. >In this case one can write:
  35. >
  36. >class F {
  37. >   ...
  38. >   bool predicate(int); // instead of bool operator()(int);
  39. >   int unary_operation(int); // instead of int operator()(int);
  40. >   int binary_operation(int, int); // instead of int operator()(int, int);
  41. >   bool comparison(int, int); // instead of bool operator()(int, int);
  42. >};
  43. >
  44. >and somewhere use F:
  45. >
  46. >vector<int> v1, v2;
  47. >F f;
  48. >
  49. >...
  50. >std::find_if(v1.begin(), v1.end(), f); 
  51. > // f.predicate(int) will  be called
  52. >std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f);
  53. > // f.binary_operation(int, int) will be called
  54. >std::equal(v1.begin(), v1.end(), v2.begin(), f);
  55. > // f.comparison(int, int) will be called
  56. >
  57.  
  58.  
  59.   Igor; perhaps you are not quite clear about how functors are used. 
  60.  
  61.   They are interchangeable with ordinary function addresses; this would no 
  62. longer be possible if you were to replace operator() with a member 
  63. function called, for example, unary_operation, because function addresses 
  64. do not have it. For example;
  65.  
  66.   bool ordinary_function( int ) ;
  67.  
  68.   std::find_if(v1.begin(), v1.end(),  ordinary_function ) ; 
  69.   // works if the algorigthm calls operator()()
  70.   // fails if the algorithm calls unary_operation()
  71.  
  72.  
  73.  
  74.    Also, there is no need to use a single functor class F in these two 
  75. cases, as you did;
  76.  
  77.  F f ;
  78.  std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f );
  79.  std::equal(v1.begin(), v1.end(), v2.begin(), f );
  80.  
  81. you would normally use different functors, depending on what you want the 
  82. algorithm to do.
  83.  
  84. The library provides many interchangeable functors, and you can build a 
  85. collection of your own ones. There is no requirement to use only one 
  86. functor in all cases.
  87.  
  88.  
  89.  
  90.      The purpose of a functor is to be a "function with attached data", as 
  91. this example makes clear;
  92.  
  93.    find_if( v.begin(),  v.end(),  IsEqualTo(6)  ) ;
  94.  
  95. The functor is effectively a function which I have customised by attaching 
  96. the number 6 to it. So, I am not obliged to write a whole set of ordinary 
  97. functions like these;
  98.  
  99.    bool IsEqualTo6( int ) ;
  100.    bool IsEqualTo7( int ) ;
  101.    bool IsEqualTo8( int ) ;
  102.    ..................
  103.  
  104.  
  105.                             David
  106. ---
  107. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  108.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  109.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  110.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  111.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  112. ]
  113.